import request from '@/utils/request'

export default {
  namespaced: true,
  state: {
    customerInfo: {},
    addressList: []
    
  },
  mutations: {
    SET_customerInfo(state, customerInfo) {
      state.customerInfo = customerInfo
    },
    SET_addressList(state, addressList) {
      state.addressList = addressList
    },
    GET_customerInfo(state) {
      return state.customerInfo
    },
    fresh_addressList(state,customerAddress){
      state.addressList.push(customerAddress);
    }
  },
  actions: {
    /**
     * 获取顾客信息
     *
     */
    async getcustomerInfo({
      commit
    }) {
      await request.get('/schoolOrder/app/getCustomerByToken?token=' + localStorage.getItem('token')).then(
        res => {
          sessionStorage.setItem("username", res.data.data.username)
          commit('SET_customerInfo', res.data.data)
        }
      )
    },

    /**
     * 
     * 更新顾客信息
     * @param {*} params 
     */
    async updateCustomerInfo({
      commit
    }, params) {
      await request.post('/schoolOrder/app/updateCustomer', {
        id: params.customerInfoTemp.id,
        createBy: params.customerInfoTemp.createBy,
        createTime: params.customerInfoTemp.createTime,
        updateBy: params.customerInfoTemp.updateBy,
        updateTime: params.customerInfoTemp.updateTime,
        sysOrgCode: params.customerInfoTemp.sysOrgCode,
        icon: params.customerInfoTemp.icon,
        name: params.customerInfoTemp.name,
        sex: params.customerInfoTemp.sex,
        tel: params.customerInfoTemp.tel,
        username: params.customerInfoTemp.username,
        password: params.customerInfoTemp.password,
        payPassword: params.customerInfoTemp.payPassword,
        balance:params.customerInfoTemp.balance
      }).then(
        res => {
          localStorage.setItem("token", res.data.data);
        }
      )
    },

    /**
     * 
     * 获取用户地址信息
     *  
     */
    async getAddressList({
      commit,
      state
    }) {
      await request.get('/schoolOrder/app/list', {
        params: {
          customerId: state.customerInfo.id
        }
      }).then(
        res => {
          //console.log(res);
          commit('SET_addressList', res.data.result.records)
        }
      )
    },


    /**
     * 新增地址信息
     */
    async addAddressList({
      commit,
      state
    }, params) {
      params.customerAddress.customerId = state.customerInfo.id
      await request.post('/schoolOrder/app/add', {
        customerId: state.customerInfo.id,
        name: params.customerAddress.name,
        tel: params.customerAddress.tel,
        province: params.customerAddress.province,
        city: params.customerAddress.city,
        county: params.customerAddress.county,
        addressDetail: params.customerAddress.addressDetail,
        address: params.customerAddress.address,
        isDefault: params.customerAddress.isDefaultTemp,
      }).then(
        res => {
          commit('fresh_addressList',params.customerAddress)
          return res;
        }
      )
    },


    /**
     * 删除地址信息
     */
    async deleteAddressList({
      commit
    }, params) {
      await request.get('/schoolOrder/app/delete', {
        params: {
          id: params.id
        }
      }).then(
        res => {
          return res;
        }
      )
    },

    /**
     * 更新地址信息
     */
    async updateAddressList({
      commit
    }, params) {
      await request.post('/schoolOrder/app/edit', {
        // customerAddress: params.customerAddress
        id: params.customerAddress.id,
        name: params.customerAddress.name,
        tel: params.customerAddress.tel,
        province: params.customerAddress.province,
        city: params.customerAddress.city,
        county: params.customerAddress.county,
        addressDetail: params.customerAddress.addressDetail,
        address: params.customerAddress.address,
        isDefault: params.customerAddress.isDefaultTemp,
      }).then(
        res => {
          return res;
        }
      )
    }

  },

}